<?php

/**
 * This is the model class for table "{{book}}".
 *
 * The followings are the available columns in table '{{book}}':
 * @property string $id
 * @property string $title
 * @property string $thumb
 * @property string $description
 * @property string $content
 * @property integer $people
 * @property integer $click
 * @property integer $good
 * @property integer $status
 * @property integer $create_time
 * @property integer $listorder
 * @property integer $gid
 * @property integer $cid
 * @property integer $is_recommend
 * @property integer $is_book
 */
class Book extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{book}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('people, click, good, status, create_time, listorder, gid, cid, is_recommend, is_book', 'numerical', 'integerOnly'=>true),
			array('title', 'length', 'max'=>40),
			array('thumb', 'length', 'max'=>120),
			array('description', 'length', 'max'=>255),
			array('content', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('id, title, thumb, description, content, people, click, good, status, create_time, listorder, gid, cid, is_recommend, is_book', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'ga'=>array(self::BELONGS_TO, 'Grade', 'gid'),
            'ca'=>array(self::BELONGS_TO, 'Classes', 'cid')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Title',
			'thumb' => 'Thumb',
			'description' => 'Description',
			'content' => 'Content',
			'people' => 'People',
			'click' => 'Click',
			'good' => 'Good',
			'status' => 'Status',
			'create_time' => 'Create Time',
			'listorder' => 'Listorder',
			'gid' => 'Gid',
			'cid' => 'Cid',
			'is_recommend' => 'Is Recommend',
			'is_book' => 'Is Book',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('thumb',$this->thumb,true);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('content',$this->content,true);
		$criteria->compare('people',$this->people);
		$criteria->compare('click',$this->click);
		$criteria->compare('good',$this->good);
		$criteria->compare('status',$this->status);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('listorder',$this->listorder);
		$criteria->compare('gid',$this->gid);
		$criteria->compare('cid',$this->cid);
		$criteria->compare('is_recommend',$this->is_recommend);
		$criteria->compare('is_book',$this->is_book);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Book the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
    /**
     * 通过category_id  获取此分类里面的share信息
     * @param $category_id
     * @param $first
     * @param $total
     * @return mixed
     */
    public function getInfo($first,$total,$type=1,$is_recommend='',$gid=''){
        $db = Yii::app()->db->createCommand();
        $db->select('id,title,thumb');
        $db->from('{{book}}');
        $db->andWhere('status=:status',
            array(':status'=>1));
        if($type){
            if($type==2){
                $type = 0;
            }
            $db->andWhere('is_book=:is_book',array(':is_book'=>$type));
        }
        if($gid){
            $db->andWhere('gid=:gid',array(':gid'=>$gid));
        }
        if($is_recommend){
            $db->andWhere('is_recommend=:t',array(':t'=>1));
        }
        $db->order('create_time desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
    public function getGuideInfo($first,$total){
        $db = Yii::app()->db->createCommand();
        $db->select('id,title,thumb');
        $db->from('{{book}}');
        $db->andWhere('status=:status and content !=:content',
            array(':status'=>1,':content'=>''));
        $db->order('create_time desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }



    public function getHotInfo($first,$total,$gid='',$cid='',$keyword=''){
        $db = Yii::app()->db->createCommand();
        $db->select('id,title,thumb,people,good,click');
        $db->from('{{book}}');
        $db->andWhere('status=:status',
            array(':status'=>1));
        if($gid){
            $db->andWhere('gid=:gid',array(':gid'=>$gid));
        }
        if($cid){
            $db->andWhere('cid=:t',array(':t'=>$cid));
        }
        if($keyword){
            $db->andWhere(array('like', 'title', '%'.$keyword.'%'));
        }
        $db->order('click desc,id desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
    public function getReadInfo($first,$total,$gid='',$cid=''){
        $db = Yii::app()->db->createCommand();
        $db->select('id,title,thumb,people,good,click');
        $db->from('{{book}}');
        $db->andWhere('status=:status',
            array(':status'=>1));
        if($gid){
            $db->andWhere('gid=:gid',array(':gid'=>$gid));
        }
        if($cid){
            $db->andWhere('cid=:t',array(':t'=>$cid));
        }
        $db->order('people desc,id desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }
    public function getLikeInfo($first,$total,$gid='',$cid=''){
        $db = Yii::app()->db->createCommand();
        $db->select('id,title,thumb,people,good,click');
        $db->from('{{book}}');
        $db->andWhere('status=:status',
            array(':status'=>1));
        if($gid){
            $db->andWhere('gid=:gid',array(':gid'=>$gid));
        }
        if($cid){
            $db->andWhere('cid=:t',array(':t'=>$cid));
        }
        $db->order('good desc,id desc');
        $db->limit($total,$first);
        $info = $db->queryAll();
        return $info;
    }

}
